#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class Web_MessagesProvider : CY.GFive.Core.Providers.DALProvider.IWeb_MessagesProvider
	{
		#region Sql
		private static readonly string SqlSelectWeb_Messages = "SELECT [Id], [Message_Title],[Message_Content],[Message_PubDate],[Manager_Code] FROM [Web_Messages] WHERE  [Id]=@Id";

		private static readonly string SqlInsertWeb_Messages = "INSERT INTO [Web_Messages]([Message_Title],[Message_Content],[Message_PubDate],[Manager_Code]) values(@Message_Title,@Message_Content,@Message_PubDate,@Manager_Code);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateWeb_Messages = "UPDATE [Web_Messages] SET [Message_Title] = @Message_Title,[Message_Content] = @Message_Content,[Message_PubDate] = @Message_PubDate,[Manager_Code] = @Manager_Code WHERE  [Id]=@Id";

		private static readonly string SqlDeleteWeb_Messages = "DELETE [Web_Messages] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllWeb_Messages = "SELECT [Id], [Message_Title],[Message_Content],[Message_PubDate],[Manager_Code] FROM [Web_Messages]";
		#endregion

		#region IWeb_MessagesProvider Members
		public Core.Business.Web_Messages Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectWeb_Messages);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.Web_Messages web_Messages = new Core.Business.Web_Messages();

				if (!reader.IsDBNull(0)) web_Messages.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) web_Messages.Message_Title = reader.GetString(1);
				if (!reader.IsDBNull(2)) web_Messages.Message_Content = reader.GetString(2);
				if (!reader.IsDBNull(3)) web_Messages.Message_PubDate = reader.GetDateTime(3);
				if (!reader.IsDBNull(4)) web_Messages.Manager_Code = reader.GetString(4);

				return web_Messages;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.Web_Messages web_Messages)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Message_Title", SqlDbType.NVarChar, web_Messages.Message_Title);
			sql.AddParameter("@Message_Content", SqlDbType.NVarChar, web_Messages.Message_Content);
			sql.AddParameter("@Message_PubDate", SqlDbType.DateTime, web_Messages.Message_PubDate);
			sql.AddParameter("@Manager_Code", SqlDbType.NVarChar, web_Messages.Manager_Code);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertWeb_Messages);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) web_Messages.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.Web_Messages web_Messages)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Message_Title", SqlDbType.NVarChar, web_Messages.Message_Title);
			sql.AddParameter("@Message_Content", SqlDbType.NVarChar, web_Messages.Message_Content);
			sql.AddParameter("@Message_PubDate", SqlDbType.DateTime, web_Messages.Message_PubDate);
			sql.AddParameter("@Manager_Code", SqlDbType.NVarChar, web_Messages.Manager_Code);
			sql.AddParameter("@Id", SqlDbType.Int, web_Messages.Id);
			sql.ExecuteSql(SqlUpdateWeb_Messages);
		}

		public void Delete(Core.Business.Web_Messages web_Messages)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, web_Messages.Id);
			sql.ExecuteSql(SqlDeleteWeb_Messages);
		}

		public IList<Core.Business.Web_Messages> GetAllWeb_Messages()
		{
			IList<Core.Business.Web_Messages> web_Messageslist = new List<Core.Business.Web_Messages>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllWeb_Messages);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.Web_Messages web_Messages = new Core.Business.Web_Messages();

					if (!reader.IsDBNull(0)) web_Messages.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) web_Messages.Message_Title = reader.GetString(1);
					if (!reader.IsDBNull(2)) web_Messages.Message_Content = reader.GetString(2);
					if (!reader.IsDBNull(3)) web_Messages.Message_PubDate = reader.GetDateTime(3);
					if (!reader.IsDBNull(4)) web_Messages.Manager_Code = reader.GetString(4);

					web_Messages.MarkOld();
					web_Messageslist.Add(web_Messages);
				}
				reader.Close();
			}
			return web_Messageslist;
		}
		#endregion
	}
}
